package com.boyuan.huijiaoyu;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceActivity.Header;
import android.provider.SyncStateContract.Helpers;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMMessage;
import com.example.contactfragment.ContactService;
import com.example.contactfragment.LognUser;
import com.example.huanxin.more.DemoHXSDKHelper;
import com.example.login.LoginApp1;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.StringUtils;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.constant.Constant;
import com.ttcaca.cas.android.client.model.User;

/**
 * 登录界面
 */
public class AccountLoginActivity extends BaseActivity implements
OnClickListener {

	private static final String TAG = "AccountLoginActivity";

	private Button btn_login;// 登陆
	private EditText edit_mobile,// 手机号码输入框
	edit_pwd;// 密码输入框
	private String mobile;
	private String pwd;
	User account;
	private ContactService contactService;
	boolean isdenglu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_login_activity);
		findViewById(R.id.lay_delname).setOnClickListener(this);
		isdenglu = false;
		Intent intent = getIntent();
		if (intent != null) {
			username = intent.getStringExtra("mobile");
		}else{
			username = "";
		}
		SharedPreferences sp = getSharedPreferences("user.xml", 0);
		boolean islogin = sp.getBoolean("islogin", false);
		if(islogin){
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					loginMainActivity();
				}
			});
		}
		prepareView();

	}

	private void getString(String string) {
		// TODO Auto-generated method stub
		
	}

	private void prepareView() {

		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(this);
		findViewById(R.id.btn_zhuce).setOnClickListener(this);
		findViewById(R.id.lay_zhaohuimima).setOnClickListener(this);
		edit_mobile = (EditText) findViewById(R.id.edit_mobile);
		edit_mobile.setText(username);
		edit_pwd = (EditText) findViewById(R.id.edit_pwd);
		account = SharePreferenceHelper.GetAccount(this);
	}

	// 登陆
	private void doLogin() {
		mobile = edit_mobile.getText().toString().trim();
		pwd = edit_pwd.getText().toString().trim();
		SharedPreferences sp = getSharedPreferences("mima.xml", 0);
		Editor edit = sp.edit();
		edit.putString("username", mobile);
		edit.putString("mima", pwd);
		edit.commit();
		md5 = getMD5(pwd);

		if (StringUtils.isEmpty(mobile)) {
			ToastHelper.showMsg(AccountLoginActivity.this, "帐号不能为空", false);
			return;
		}

		if (StringUtils.isEmpty(pwd)) {
			ToastHelper.showMsg(AccountLoginActivity.this, "密码不能为空", false);
			return;
		}
		
		loginActivity();
	}

	private void loginActivity() {
		if (contactService == null) {
			contactService = new ContactService(this);
		}
		
		contactService.getdenglu1(mobile, md5, new GetMultilParamsListener() {
			
			private String id;

			@Override
			public void onFinish(Object... params) {
				
				if (params != null ) {
					LognUser lognUser =(LognUser) params[0];
					String account = lognUser.getAccount();
					id = lognUser.getId();
					String mail = lognUser.getMail();
					String mobile = lognUser.getMobile();
					String register_time = lognUser.getRegister_time();
					String state = lognUser.getState();
					String password = lognUser.getPassword();
					SharedPreferences sp = getSharedPreferences("user.xml", 0);
					Editor edit = sp.edit();
//					edit.putString("yonghu", account);
					edit.putString("account_id", id);
					edit.putString("mail", mail);
					edit.putString("mobile", mobile);
					edit.putString("register_time", register_time);
					edit.putString("state", state);
					edit.putString("password", password);
					edit.putString("account", account);
					edit.putBoolean("islogin", true);
					edit.commit();
					getemployee(id);
			}
				
			}

			@Override
			public void onFailed(String Message) {
				isdenglu = false;
				ToastHelper.showMsg(AccountLoginActivity.this, Message, false);
			}
		});
	}
	
	
	public void getemployee(String id) {
		if (contactService == null) {
			contactService = new ContactService(this);
		}
		contactService.getEmployee(id, new GetMultilParamsListener() {
			
			@Override
			public void onFinish(Object... params) {
				if (params != null ) {
					LoginApp1 employee =(LoginApp1) params[0];
					String account2 = employee.getAccount();
					String accountId = employee.getAccountId();
					String address = employee.getAddress();
					String createtime = employee.getCreatetime();
					String education = employee.getEducation();
					String familyId = employee.getFamilyId();
					String familyName = employee.getFamilyName();
					String id = employee.getId();
					String idCardNo = employee.getIdCardNo();
					String isleave = employee.getIsleave();
					String jobId = employee.getJobId();
					String jobName = employee.getJobName();
					String jobNumber = employee.getJobNumber();
					String landLine = employee.getLandLine();
					String leavetime = employee.getLeavetime();
					String mail = employee.getMail();
					String mobile = employee.getMobile();
					String realname = employee.getRealname();
					String sex = employee.getSex();
					SharedPreferences sp = getSharedPreferences("a.xml", 0);
					Editor edit = sp.edit();
					edit.putString("account2", account2);
					edit.putString("accountId", accountId);
					edit.putString("username", mobile);
					edit.putString("jobname", jobName);
					edit.putString("realname", realname);
					edit.putString("mail", mail);
					edit.putString("familyid", familyId);
					edit.putString("familyname", familyName);
					edit.putString("address", address);
					edit.putString("createtime", createtime);
					edit.putString("education", education);
					edit.putString("id", id);
					edit.putString("idCardNo", idCardNo);
					edit.putString("isleave", isleave);
					edit.putString("jobNumber", jobNumber);
					edit.putString("jobId", jobId);
					edit.putString("landLine", landLine);
					edit.putString("leavetime", leavetime);
					edit.putString("sex", sex);
					edit.commit();
					Intent intent = new Intent();
					intent.setAction(Constant.FirstChangePersonHeadIcon);
					sendBroadcast(intent );
					loginMainActivity();
			}
			}
			
			@Override
			public void onFailed(String Message) {
				isdenglu = false;
			}
		});
	}

	public String getMD5(String info)
	{
		try
		{
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(info.getBytes("UTF-8"));
			byte[] encryption = md5.digest();

			StringBuffer strBuf = new StringBuffer();
			for (int i = 0; i < encryption.length; i++)
			{
				if (Integer.toHexString(0xff & encryption[i]).length() == 1)
				{
					strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
				}
				else
				{
					strBuf.append(Integer.toHexString(0xff & encryption[i]));
				}
			}

			return strBuf.toString();
		}
		catch (NoSuchAlgorithmException e)
		{
			return "";
		}
		catch (UnsupportedEncodingException e)
		{
			return "";
		}
	}

	private void loginMainActivity() {
//		boolean loginedHX = DemoHXSDKHelper.getInstance().isLogined();
//		if(loginedHX == true){
//			Toast.makeText(AccountLoginActivity.this, "此账号已在别的设备上登录", Toast.LENGTH_SHORT).show();
//		}else{
			
			Intent intent = new Intent(AccountLoginActivity.this,MainActivity.class) ;
			startActivity(intent);
			
			SharedPreferences sp = getSharedPreferences("user.xml", 0);
			String user = sp.getString("mobile", "18030095903");
			String pws = "" + user.substring(5, user.length());
			EMChatManager.getInstance().login(user, pws, new EMCallBack() {
				
				@Override
				public void onSuccess() {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onProgress(int arg0, String arg1) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onError(int arg0, String arg1) {
					// TODO Auto-generated method stub
					
				}
			});
			savePhoneName();
			finish();
//		}
		
	}

	private void savePhoneName() {
		SharedPreferences sp = getSharedPreferences("huijiaoyu", 0);
		Editor edit = sp.edit();
		edit.putString("18850198097", "赖冠文");
		edit.putString("15342492166", "沈杨炎昊");
		edit.putString("15060039250", "孙银燕");
		edit.putString("18901232453", "宫文学");
		edit.putString("18650003603", "支立");
		edit.putString("13799380539", "郑旭丹");
		edit.putString("18020709355", "许伟协");
		edit.putString("13616028364", "黄诗琦");
		edit.putString("13763832055", "林肖玲");
		edit.putString("18959241946", "黄进强");
		edit.putString("13738029984", "游健超");
		edit.putString("18010140796", "黄赐妹");
		edit.putString("18250813004", "陈聪智");
		edit.putString("13779916683", "陈健");
		edit.putString("13489179671", "陈思颖");
		edit.putString("13395987884", "付钦诚");
		edit.putString("18120786939", "翁景弘");
		edit.putString("13559145730", "郑蓉");
		edit.putString("18030040418", "朱伟平");
		edit.putString("15959271355", "卓樾");
		edit.putString("13959151242", "刘少峰");
		edit.putString("13159135260", "杨锦輝");
		edit.putString("13599540535", "吴琼");
		edit.putString("15280049521", "陈广平");
		edit.putString("18039847863", "曹拓");
		edit.putString("18259221450", "叶建军");
		edit.putString("18760425681", "曾水琴");
		edit.putString("13850622890", "蒋源德");
		edit.putString("13859994238", "洪雅斌");
		edit.putString("15659563763", "林赡平");
		edit.putString("18059833136", "黄滨滨");
		edit.putString("13400617170", "姜艳");
		edit.putString("17805916792", "刘佳_");
		edit.putString("15079280867", "熊文輝");
		edit.putString("15605923229", "朱芹香");
		edit.putString("15980216356", "何正伍");
		edit.putString("13606902529", "许瑞杰");
		edit.putString("13459299921", "谢秋佳");
		edit.putString("15980690039", "宋明媛");
		edit.putString("15859892582", "冯洁影");
		edit.putString("13896950575", "闻春燕");
		edit.putString("18850709795", "陈烨");
		edit.putString("18585022201", "刘胜");
		edit.putString("13959766015", "邱志英");
		edit.putString("18359620521", "林英");
		edit.putString("18959295413", "曾震輝");
		edit.putString("15608658701", "陈立婷");
		edit.putString("17705024625", "陈祥英");
		edit.putString("15860766932", "Nanton");
		edit.putString("18850548825", "周珺");
		edit.putString("17805938793", "卢雅芳");
		edit.putString("15818579442", "张文斌");
		edit.putString("15860727332", "赵宇飞");
		edit.putString("13645967785", "陈芝灵");
		edit.putString("18659189570", "吴金林");
		edit.putString("18750933626", "王晓梅");
		edit.putString("15060125770", "付诗荧");
		edit.putString("13616009852", "张杰");
		edit.putString("18650161683", "张泽文");
		edit.putString("18359106207", "吴婉玲");
		edit.putString("15359881307", "杨伟智");
		edit.putString("18973141449", "胡艺");
		edit.putString("13559170200", "孙艳红");
		edit.putString("15110288318", "林薇");
		edit.putString("13554465822", "蒋春艳");
		edit.putString("13959193314", "江蕊");
		edit.putString("13313850226", "王卿");
		edit.putString("18850566307", "翁妍慧");
		edit.putString("13107920412", "杨玉婷");
		edit.putString("18217451921", "陈欣");
		edit.putString("13872530930", "孙晶晶");
		edit.putString("13215001279", "林斌");
		edit.putString("13701210852", "邹小祥");
		edit.putString("18059738999", "林婷");
		edit.putString("15005056307", "林晓君");
		edit.putString("13120354967", "吴琼");
		edit.putString("18659360115", "雷传洁");
		edit.putString("15280418402", "方欣滢");
		edit.putString("13671085510", "夏淼");
		edit.putString("15859343537", "陈慧芳");
		edit.putString("13960901413", "吴艳");
		edit.putString("18030095903", "袁小丽");
		edit.putString("18060316011", "林艳妮");
		edit.putString("15005934100", "郑诗婷");
		edit.putString("18065220309", "林凤丽");
		edit.putString("18250802820", "陈文颖");
		edit.putString("18750924612", "陈淑平");
		edit.putString("18650195329", "谢心怡");
		edit.putString("15880982730", "陈莺玉");
		edit.putString("13559139963", "陈华");
		edit.putString("15980647260", "黄丽明");
		edit.putString("15859064906", "林琴英");
		edit.putString("18965004555", "陈曼曼");
		edit.putString("18928351153", "李海艳");
		edit.putString("13959240247", "李小青");
		edit.putString("18750718136", "陈巧巧");
		edit.putString("15210999127", "王雪娇");
		edit.putString("18750931260", "卢晓雪");
		edit.putString("18459133187", "薛豪丽");
		edit.putString("13859739667", "刘小梅");
		edit.putString("18750939286", "徐琦");
		edit.putString("13559440581", "姚文静");
		edit.putString("18950424992", "张晓莉");
		edit.putString("15280233191", "洪小云");
		edit.putString("18559908660", "刘梦丝");
		edit.putString("18606001889", "秦银霞");
		edit.putString("18750938768", "方榕");
		edit.putString("15960292478", "陈水斌");
		edit.putString("13716130841", "李金娜");
		edit.putString("13696989278", "黄磊");
		edit.putString("13123188602", "陈晓枫");
		edit.putString("13779944823", "钟晓琼");
		edit.putString("18850458198", "王晓瑜");
		edit.putString("18960622574", "林梦婷");
		edit.putString("15960268577", "李惠玲");
		edit.putString("18659873696", "肖梦云");
		edit.putString("13950499089", "陈敏");
		edit.putString("13799751902", "张小霞");
		edit.putString("18750931207", "朱云");
		edit.putString("18701474556", "尉亚昆");
		edit.putString("15201185267", "闫飞飞");
		edit.putString("18200255859", "俞思宇");
		edit.putString("13811138804", "臧伟东");
		edit.putString("13400609269", "陈艺红");
		edit.putString("15705923914", "成于思");
		edit.putString("18201329133", "姜玲");
		edit.putString("15080310684", "曾闽");
		edit.putString("18965399377", "熊梦怡");
		edit.putString("15120037428", "李丽丽");
		edit.putString("15001258018", "孟琳");
		edit.putString("13030847026", "许冰妹");
		edit.putString("13521834462", "郭薇");
		edit.putString("18050666020", "黄家晖");
		edit.putString("15960373613", "杨泉元");
		edit.putString("15369326725", "刘小雪");
		edit.putString("13306032311", "卢琼瑜");
		edit.putString("15659523959", "许雅清");
		edit.putString("13521710554", "郝万良");
		edit.putString("18750251369", "但盈含");
		edit.putString("18859209509", "李淼");
		edit.putString("18500588591", "彭晓见");
		edit.putString("18659129100", "李晓敏");
		edit.putString("15960817305", "王智");
		edit.putString("15388578958", "赵倩倩");
		edit.putString("13665012826", "胡雯倩");
		edit.putString("18850223630", "梁海敏");
		edit.putString("13693582335", "金颖");
		edit.putString("18650107592", "王快");
		edit.putString("15060776173", "郭霖燕");
		edit.putString("13581689560", "黄妍欣");
		edit.putString("13799795803", "唐昱");
		edit.putString("18064085297", "李晓旭");
		edit.putString("13011298401", "侯丽霞");
		edit.putString("15860739332", "吴德缘");
		edit.putString("13720899313", "江钦原");
		edit.putString("13683551727", "李萍");
		edit.putString("18950425662", "苏永华");
		edit.putString("15960270426", "陈黄嘉");
		edit.putString("15901083529", "赵新雨");
		edit.putString("18650629090", "江腾");
		edit.putString("15260509519", "黄秀娥");
		edit.putString("18611505268", "齐卫华");
		edit.putString("18759214670", "郭周铭");
		edit.putString("15213690329", "程丹平");
		edit.putString("18910122577", "景亚敏");
		edit.putString("18559233332", "钟瑞坤");
		edit.putString("15705925991", "卓园园");
		edit.putString("18710155163", "赵刘洋");
		edit.putString("18565328640", "刘颖");
		edit.putString("13950173369", "叶欣欣");
		edit.putString("13021288808", "郭慧超");
		edit.putString("13779954630", "刘頔");
		edit.putString("18250888812", "郑正鹏");
		edit.putString("13977967067", "陈骏雄");
		edit.putString("18359336790", "庄英红");
		edit.putString("15059619093", "杨雪琼");
		edit.putString("15835985585", "崔卫娜");
		edit.putString("18588766697", "余羽洁");
		edit.putString("18674506521", "徐玲");
		edit.putString("15010252861", "贾巍");
		edit.putString("13606007594", "周叶琴");
		edit.putString("15080326650", "吴彬妮");
		edit.putString("13161582667", "孟雁飞");
		edit.putString("18359181587", "陈珍惠");
		edit.putString("18850566130", "张圣榕");
		edit.putString("18201318124", "王春");
		edit.putString("18050073747", "翁丽娟");
		edit.putString("18050013765", "陈郑楠");
		edit.putString("18813011349", "许琴");
		edit.putString("13950170339", "郑阳");
		edit.putString("18202767006", "郑雅玲");
		edit.putString("18611410016", "杨狄");
		edit.putString("18049966336", "杨光");
		edit.putString("13775622263", "徐茂林");
		edit.putString("18601965162", "赵清");
		edit.putString("13618607862", "叶琦");
		edit.putString("18861263066", "王星");
		edit.putString("18810714059", "肖云");
		edit.putString("15880286271", "吴永杰");
		edit.putString("13186690187", "田佳玲");
		edit.putString("13810244656", "陈丹");
		edit.putString("18965193693", "杨成");
		edit.putString("15961252282", "王丽娜");
		edit.putString("13071131241", "滕婉蓉");
		edit.putString("15860805852", "章尚忠");
		edit.putString("13906125725", "俞飞");
		edit.putString("18698825293", "刘俊");
		edit.putString("13811459546", "郗冠媛");
		edit.putString("15051902029", "王洁");
		edit.putString("13478451511", "张强");
		edit.putString("13950090291", "陈李");
		edit.putString("13641506507", "王娟");
		edit.putString("18811478739", "杨静");
		edit.putString("13400605631", "马莉");
		edit.putString("13961188343", "金毅");
		edit.putString("15985835223", "魏倩兰");
		edit.putString("18505201753", "戴翔");
		edit.putString("13600990452", "黄巧玲");
		edit.putString("13665067226", "任文霞");
		edit.putString("18206053009", "陈华");
		edit.putString("18046048897", "蒋芳芳");
		edit.putString("13609553740", "汪诗琪");
		edit.putString("13950284122", "周展");
		edit.putString("13459163836", "连雅妮");
		edit.putString("15005936648", "蓝燕瑜");
		edit.putString("13295923558", "沈志成");
		edit.putString("13489089234", "薛娟");
		edit.putString("18938096868", "董东");
		edit.putString("13971393183", "余志斌");
		edit.putString("18850527247", "许晓新");
		edit.putString("18695690933", "陈嘉桦");
		edit.putString("18859220340", "陈章贤");
		edit.commit();
	}

	@Override
	protected void onResume() {
		super.onResume();
		doubleBackToExitPressedOnce = false;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			if(isdenglu == false){
				isdenglu = true;
				doLogin();
			}else{
//				Toast.makeText(AccountLoginActivity.this, "正在登录中...", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btn_zhuce:
//			zhuce();
			break;
		case R.id.lay_zhaohuimima:
			startActivity(new Intent(AccountLoginActivity.this,ZhaohuimimaActivity.class));
			break;
		case R.id.lay_delname:
			edit_mobile.setText("");
			break;
		default:
			break;
		}
	}

	private void zhuce() {
		startActivity(new Intent(this,ZhuceYonghuActivity.class));
	}

	// 两次返回后退出应用
	private boolean doubleBackToExitPressedOnce = false;

	@Override
	public void backClicked() {

		Log.i(TAG, "backClicked");

		if (!isShowingLoading()) {

			if (!doubleBackToExitPressedOnce) {
				this.doubleBackToExitPressedOnce = true;
				mHandler.sendEmptyMessageDelayed(0, 2000);  
				ToastHelper.showMsg(AccountLoginActivity.this, getString(R.string.exit_press_back_twice_message), false);
			}else {

				isBackAllowed = true;
				Log.i(TAG, "isBackAllowed");
				setResult(RESULT_CANCELED);
			}
		}else {
			hideLoading();
		}
	}

	private Handler mHandler = new Handler() {  
		@Override  
		public void handleMessage(Message msg) {  
			super.handleMessage(msg);  
			doubleBackToExitPressedOnce = false;  
		}  
	};

	private String md5;

	private String username; 
}
